/*
 * BSM6.0
 *
 * Copyright 2012-2013 (C) SINODATA CO., LTD. All Rights Reserved.
 */
package com.sinodata.bsm.cicp.finder.IIS6;

import java.io.BufferedReader;
import java.io.IOException;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.finder.Finder;
import com.sinodata.bsm.cicp.util.CommandReader;

/**
 * <p>
 * Description: 该类主要提供 获取主机名称的方法
 * </p>
 *
 * @author wangpeng
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-6-4 上午11:12:34          wangpeng        1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
public abstract class IIS6Finder extends Finder {
    private static final Logger logger = Logger.getLogger(IIS6Finder.class);
    private static final String HOSTNAME_CMDSTR = "hostname";
    private String HOSTNAME = "";

    protected static final int WINDOWS_FINDER_DEFAULT_TIMEOUT = 5;

    /**
     * 获取主机名称
     * 在Windows主机资源发现的时候,设置主机的资源描述为:主机名_资源描述
     * @return String
     */
    protected final String getHostName(String ip) {
        if (!HOSTNAME.equals("")) {
            return HOSTNAME;
        }
        BufferedReader reader = null;
        String strLine = null;
        try {
            reader = CommandReader.getReader(ip, HOSTNAME_CMDSTR, 0, 0);
            if ((strLine = reader.readLine()) != null) {
                HOSTNAME = strLine.trim();
            }
        } catch (IOException e) {
            logger.error("failed to get hostname ", e);
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    logger.error("failed to close reader", e);
                }
            }
        }
        return HOSTNAME;
    }
}
